package com.dao.basic.support;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.dao.basic.IModuleUsersDao;
import com.dao.spring.BaseDao;
import com.pojo.basic.ModuleUsers;

public class ModuleUsersDao extends BaseDao implements IModuleUsersDao{

	public void sava(ModuleUsers moduleUsers) {
		super.getHibernateTemplate().save(moduleUsers);
	}

	@SuppressWarnings("unchecked")
	public  List<ModuleUsers> findModuleUsersByModuleId(Integer moduleId) {
		return super.getHibernateTemplate().find("from ModuleUsers as mu where mu.MODULE_ID='"+moduleId+"'");
	}
	
	@SuppressWarnings("unchecked")
	public List<ModuleUsers> findModuleUsersByModuleIdAndUsers(Integer moduleId, String pswdUsr) {
		return super.getHibernateTemplate().find("from ModuleUsers as mu where mu.MODULE_ID='"+moduleId+"' and mu.PSWD_USR='"+pswdUsr+"'");
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void delete(final Integer moduleId) {
		super.getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session)throws HibernateException, SQLException {
				session.createQuery("delete from ModuleUsers as mu where mu.MODULE_ID="+moduleId+"").executeUpdate();
				return null;
			}
		});
	}
	
}
